// pages/member/shop/index.js
import api from "../../../api/index";
const App = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //#region 设置搜索框
    searchList: [
      {
        name: "shop_id",
        type: "topShop",
        offRefresh: true,
      },
      {
        name: "topDate",
        names: ["time_form", "start_date", "end_date"],
        type: "topDate",
      },
      {
        name: "search",
        type: "input",
        label: "会员姓名、手机号",
      },
      {
        name: "vip_status",
        type: "picker",
        label: "会员状态",
      },
      {
        name: "assign_coach_id",
        type: "pickerCoach",
        label: "分配教练",
        span: 7,
      },
      {
        name: "assign_form",
        type: "picker",
        label: "分配",
        span: 5,
      },
      {
        name: "guide_coach_id",
        type: "pickerCoach",
        label: "指导教练",
        span: 7,
      },
      {
        name: "guide_form",
        type: "picker",
        label: "指导",
        span: 5,
      },
      {
        name: "vip_course",
        type: "picker",
        label: "是否有课",
        span: 8,
      },
      {
        name: "vip_source",
        type: "picker",
        label: "会员来源",
        span: 8,
      },
    ],

    searchNum: [
      {
        label: "总人数(人)",
        show: true,
        num: 0,
      },
    ],

    sExplain: {
      title: "搜索说明",
      content: "时间可以选择，对会员名称、手机号、会员类型、分配教练、分配状态、指导教练、指导状态、是否有课、会员来源进行搜索",
    },

    nExplain: {
      title: "统计说明",
      content: "根据搜索条件对门店会员总人数进行统计",
    },

    searchParam: {
      time_form: "month",
    },
    //#endregion

    //#region 列表
    list: [],
    isMore: true,
    page: 1,
    isLoading: false,
    //#endregion

    //#region 教练
    coachShow: false,
    userID: null,
    userIndex: null,
    coachObj: {
      name: "coach_id",
    },
    //#endregion
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      list: [],
      isMore: true,
      page: 1,
      "searchParam.shop_id": wx.getStorageSync("shopID"),
    });
    this.getCoachList();
    this.getDataList();
  },
  getCoachList() {
    api.apiStoreCoachList().then((res) => {
      App.globalData.coachList = res.data.list;
      this.setData({
        "coachObj.list": res.data.list,
      });
    });
  },
  openCoach(e) {
    this.setData({
      userID: e.currentTarget.dataset.id,
      userIndex: e.currentTarget.dataset.i,
      coachShow: true,
    });
  },
  closeCoach() {
    this.setData({
      coachShow: false,
    });
  },
  getCoach(e) {
    let { userID, coachObj, list, userIndex } = this.data;
    let coachList = coachObj.list;
    api
      .apiStoreAssign({
        user_id: userID,
        coach_id: e.detail.value,
      })
      .then((res) => {
        if (res.code === 1) {
          if (userID === list[userIndex].user_info.user_id) {
            list[userIndex].assign_form = "have";
            list[userIndex].assign_str = coachList.find(
              (i) => i.coach_id === e.detail.value
            ).nickname;
            this.setData({
              list,
            });
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      });
  },
  searchChonge(e) {
    this.setData({
      searchParam: e.detail,
      list: [],
      isMore: true,
      page: 1,
    });
    this.getDataList();
  },
  getDataList() {
    let { page, list, searchParam, searchNum } = this.data;
    searchParam = {
      ...searchParam,
    };
    this.setData({
      isLoading: true,
    });
    api
      .apiStoreVipList({
        ...searchParam,
        page,
      })
      .then((res) => {
        if (
          JSON.stringify(this.data.searchParam) !== JSON.stringify(searchParam)
        ) {
          return;
        }

        let data = res.data;
        if (page === 1) {
          searchNum[0].num = data.entirety.people;
          this.setData({
            searchNum,
            "searchParam.shop_id": data.shop_info.shop_id,
          });
        }
        if (page >= data.all_page) this.setData({ isMore: false });
        if (data.list.length) {
          data.list = data.list.map((i) => {
            i.user_info.tagStr = i.vip_str;
            i.user_info.tagVal = i.is_vip;
            if (i.guide_form === "have") i.user_info.guide_str = i.guide_str;
            return {
              ...i,
            };
          });
          list.push(...data.list);
          this.setData({ list });
        }
      })
      .finally((_) => {
        this.setData({
          isLoading: false,
        });
      });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.isMore) return;
    if (this.data.isLoading) return;
    let page = this.data.page;
    this.setData({
      page: page + 1,
    });
    this.getDataList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
